package com.nuoniu.sibanyun.service.erp.in.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.erp.in.InRdStyle;
import com.nuoniu.sibanyun.mapper.erp.in.InRdStyleMapper;
import com.nuoniu.sibanyun.service.erp.IErpPurchaseTypeService;
import com.nuoniu.sibanyun.service.erp.IErpSaleTypeService;
import com.nuoniu.sibanyun.service.erp.in.IInRdStyleService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 出入库类型 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-25
 */
@Service
public class InRdStyleServiceImpl extends ServiceImpl<InRdStyleMapper, InRdStyle> implements IInRdStyleService {

    @Autowired
    private IErpSaleTypeService saleTypeService;
    @Autowired
    private IErpPurchaseTypeService purchaseTypeService;

    @Override
    public Page<InRdStyle> getStyleList(Page pages, InRdStyle rdStyle) {
        UserInfo userInfo = UserThreadLocal.get();
        InRdStyle style = new InRdStyle();
        QueryWrapper<InRdStyle> queryWrapper = new QueryWrapper(style);
        queryWrapper.eq("company_id", userInfo.getCompanyId());
        queryWrapper.eq("account_id", rdStyle.getAccountId());
        if(rdStyle.getType() != null) {
            queryWrapper.eq("type", rdStyle.getType());
        }
        queryWrapper.orderByAsc("code");
        return page(pages, queryWrapper);
    }

    @Override
    public void updateChildrenParentIds(InRdStyle inRdStyle, InRdStyle parentRdStyle) {
        InRdStyle rdStyle = this.getById(inRdStyle.getId());
        if(inRdStyle.getParentId() != rdStyle.getParentId()) {
            String newTreeNames = parentRdStyle.getTreeNames() + "/" + parentRdStyle.getName();
            String newParentIds = parentRdStyle.getParentIds() + parentRdStyle.getId() + ",";
            baseMapper.updateChildrenParentIds(rdStyle.getId(), rdStyle.getParentIds(), rdStyle.getTreeNames(),newParentIds, newTreeNames);
        }
    }

    @Override
    public List<InRdStyle> getStyleByParentId(Long parentId) {
        return baseMapper.getStyleByParentId(parentId);
    }

    @Override
    public InRdStyle getByCode(InRdStyle inRdStyle) {
        return baseMapper.getByCode(inRdStyle);
    }

    @Override
    public InRdStyle getByName(InRdStyle inRdStyle) {
        return baseMapper.getByName(inRdStyle);
    }

    @Override
    public void delete(Long id) {
        InRdStyle style = getById(id);
        if(style == null) {
            throw new NuoNiuException("不存在");
        }

        if(!style.getIsEndNode()) {
            throw new NuoNiuException("只能删除末级");
        }

        if(style.getType().equals(2)) {
            Integer count = saleTypeService.getByStyle(id);
            if(count > 0) {
                throw new NuoNiuException("已使用，不能删除");
            }
        }else {
            Integer count = purchaseTypeService.getByStyle(id);
            if(count > 0) {
                throw new NuoNiuException("已使用，不能删除");
            }
        }

        //删除
        removeById(id);
        //更新父级
        if(style.getParentId() != null) {
            List<InRdStyle> rdStyleList = baseMapper.getStyleByParentId(style.getParentId().longValue());
            if(rdStyleList.size() == 0) {
                InRdStyle rdStyle = new InRdStyle();
                rdStyle.setId(style.getParentId().longValue());
                rdStyle.setIsEndNode(true);
                updateById(rdStyle);
            }
        }
    }
}
